package com.example.scancodelogindemo.utils;

import cn.hutool.core.text.StrJoiner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author 傲寒
 * @date 2023/9/6
 */
@Order(Integer.MIN_VALUE)
@Component
public class RedisGenerateKeyUtils implements ApplicationRunner {

    private static String prefix = null;

    public final String applicationName;

    public final String domain;


    public RedisGenerateKeyUtils(@Value("${spring.application.name}") String applicationName,
                                 @Value("${domain}") String domain) {
        this.applicationName = applicationName;
        this.domain = domain;
    }


    @Override
    public void run(ApplicationArguments args) {
        prefix = applicationName + ":" + domain;
    }

    public static String buildKey(String key) {
        return prefix + ":" + key;
    }


    public static String buildKey(Object... keys) {
        final StrJoiner strJoiner = new StrJoiner(":", prefix + ":", "");
        for (Object key : keys) {
            if (Objects.nonNull(key)) {
                strJoiner.append(key);
            }
        }
        return strJoiner.toString();
    }

}
